package com.maori.languageapp.discover.word;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import com.maori.languageapp.R;
import com.maori.languageapp.adapter.Languageapp_Curate_AudioView_Activity;
import com.maori.languageapp.adapter.Languageapp_Curate_VideoView_Activity;
import com.maori.languageapp.consts.ImageLoader;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Maori_Discover_Word_AudioList_Adapter extends BaseAdapter {
	// Declare Variables
	Context context;
	LayoutInflater inflater;
	ArrayList<HashMap<String, String>> data;
	// Button videoview_example;
	ImageLoader imageLoader;
	private MediaPlayer mediaPlayer;
	public Maori_Discover_Word_AudioList_Adapter(Context context,
			ArrayList<HashMap<String, String>> arraylist) {
		this.context = context;
		data = arraylist;
		imageLoader = new ImageLoader(context);

	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		// Declare Variables

		TextView first_name, user_role, setLikeAction;
		ImageView flag, userimage;
		Button setDefImageAction, setLikeImageAction, urlreader,display_dialect,display_location;

		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View itemViewImages = inflater.inflate(
				R.layout.languageapp_discover_audio, parent, false);

		// Get the position from the results
		HashMap<String, String> resultp = new HashMap<String, String>();
		resultp = data.get(position);
		// Locate the ImageView in listview_item.xml
		first_name = (TextView) itemViewImages.findViewById(R.id.firstname);
		user_role = (TextView) itemViewImages.findViewById(R.id.lastname);
		display_dialect = (Button) itemViewImages.findViewById(R.id.display_dialect);
		display_location = (Button) itemViewImages.findViewById(R.id.display_location);
		/*
		 * flag = (ImageView)
		 * itemViewImages.findViewById(R.id.ImageView_example);
		 */
		userimage = (ImageView) itemViewImages
				.findViewById(R.id.userimggriditem);

		setLikeAction = (Button) itemViewImages
				.findViewById(R.id.setLikeAction);
		setDefImageAction = (Button) itemViewImages
				.findViewById(R.id.setDefAction);
		urlreader = (Button) itemViewImages.findViewById(R.id.urlreader);

		imageLoader.DisplayImage(
				resultp.get(Maori_Discover_Word_Details.IMAGEMID), userimage);
		first_name.setText(resultp
				.get(Maori_Discover_Word_Details.IMAGEfirst_name) + " " +resultp
						.get(Maori_Discover_Word_Details.IMAGElast_name));
		user_role.setText(resultp
				.get(Maori_Discover_Word_Details.SYNrole));
		display_dialect.setText(resultp
				.get(Maori_Discover_Word_Details.SYNtribe));
		display_location.setText(resultp
				.get(Maori_Discover_Word_Details.SYNcurrent_location));
		setLikeAction.setText("Like ("
				+ resultp.get(Maori_Discover_Word_Details.IMAGETotalsLikes)
				+ ")");
		// Capture button clicks on ListView items
		urlreader.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// Get the position from the results
				HashMap<String, String> resultp = new HashMap<String, String>();
				resultp = data.get(position);
				String AudioUrlSource =resultp.get(Maori_Discover_Word_Details.IMAGEM_Source);
				
				mediaPlayer = new MediaPlayer();
				mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
				try {
					mediaPlayer.setDataSource(AudioUrlSource);
					mediaPlayer.prepare();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (SecurityException e) {
					e.printStackTrace();
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				mediaPlayer.start();
				/*Log.e("REQUEST :",
						"URL *********** :"
								+ resultp
										.get(Maori_Discover_Word_Details.IMAGEM_Source));
				Intent intent = new Intent(context,
						Languageapp_Curate_AudioView_Activity.class);
				intent.putExtra("SourceURL",
						resultp.get(Maori_Discover_Word_Details.IMAGEM_Source));
				context.startActivity(intent);*/
			}
		});

		return itemViewImages;
	}

}